package cn.buk.tms.eotms.response;

import cn.buk.common.flight.dto.FlightBasicInfo;
import cn.buk.common.util.DateUtil;
import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.common.dto.CustomerDto;
import cn.buk.tms.common.dto.PaymentMethodDto;
import cn.buk.tms.common.dto.base.BaseOrderDto;
import cn.buk.tms.eotms.dto.VasOrderPassengerDto;
import cn.buk.tms.eotms.entity.FlightInfo;
import cn.buk.tms.eotms.entity.VasOrder;
import cn.buk.tms.eotms.entity.VasOrderFlight;
import cn.buk.tms.eotms.entity.VasOrderPassenger;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.BeanUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yfdai
 */
public class VasOrderResponseDto {

    public static VasOrderResponseDto createByVasOrder(VasOrder vasOrder) {
        VasOrderResponseDto dto = new VasOrderResponseDto();
        BeanUtils.copyProperties(vasOrder, dto,
                "customer",
                "supplier",
                "paymentMethod",
                "passengers",
                "flights");

        if (vasOrder.getCustomer() != null) {
            dto.setCustomer(CustomerDto.create(vasOrder.getCustomer().getId(), vasOrder.getCustomer().getVipName(), vasOrder.getCustomer().getCustomerCode()));
            dto.setCustomerId(vasOrder.getCustomer().getId());
        }



        for(VasOrderPassenger psg: vasOrder.getPassengers()) {
            VasOrderPassengerDto psgDto = new VasOrderPassengerDto();
            BeanUtils.copyProperties(psg, psgDto);

            dto.getPassengers().add(psgDto);
        }

        for(VasOrderFlight flt: vasOrder.getFlights()) {
            FlightBasicInfo info = new FlightBasicInfo();
//            BeanUtils.copyProperties(flt.getFlight(), info);
            FlightInfo old = flt.getFlight();
            info.setDport(old.getDport());
            info.setDportName(old.getDepartureAirportName());
            info.setAport(old.getAport());
            info.setAportName(old.getArrivalAirportName());
            info.setFlightNo(old.getFlightNo());
            info.setSubclass(old.getSubclass());

            try {
                info.setDdate(DateUtil.convertToDate(flt.getFlight().getDdate()));
            } catch (ParseException ignored) {
            }
            info.setDtime(old.getDtime());
            info.setAtime(old.getAtime());
            info.setDterm(old.getDterm());
            info.setAterm(old.getAterm());

            dto.getFlights().add(info);
        }

        return dto;
    }

    /**
     * 上级订单
     */
    private BaseOrderDto parentOrder;

    private int insuranceOrderType;

    private int id;

    private int enterpriseId;

    private String orderNo;

    private String externalOrderNo;

    /**
     * 关联的机票订单
     */
    private int flightOrderId;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date createDate;

    /**
     * 商品类型： 保险，门票等
     */
    private int productType;

    private String productCode;

    private String productName;

    private int customerId;

    /**
     * 成本中心
     */
    private String costCenter;

    /**
     * 项目名称
     */
    private String projectName;

    /**
     * 对应 VasOrder 中的属性
     */
    private int customerType;
    /**
     * 对应 VasOrder 中的属性
     */
    private CustomerDto customer;

    private String operator;

    private String ticketer;

    /**
     * 订单状态
     * 0 - 待处理
     * 1 - 已处理
     * 4 - 已取消
     */
    private int status;

    /**
     * 标价
     */
    private double price;

    /**
     * 折扣金额
     */
    private double discount;

    /**
     * 成本
     */
    private double cost;

    /**
     * 数量
     */
    private int count;

    private Double total;

    /**
     * 该账单客户的付款方式
     * 1-现金；
     * 2-信用卡；
     * 4-支票；
     * 8-记账
     */
    private int payType;

    /**
     * 付款状态(只有付款类型非记账方式时才有用）
     * 0 or null 未支付
     * 1 已支付
     */
    private int payStatus;

    /**
     * 订单类型
     * 0 - 本地的服务订单
     * 1 - 第三方的机场服务（VAAS)
     */
    private int orderType;

    private String remark;

    /**
     * 对应的总订单id
     */
    private Integer blanketOrderId;

    private List<VasOrderPassengerDto> passengers;

    private List<FlightBasicInfo> flights;


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastUpdate;

    public double getProfit() {
        double profit = this.getTotalAmount() -  count * cost;

        return DecimalUtil.keepScale2(profit);
    }

    public double getTotalAmount() {
        if (this.total == null) {
            this.total = count * (price - discount);
        }

        return DecimalUtil.keepScale2(this.total);
    }

    public double getTotalCost() {
        return DecimalUtil.keepScale2(count * cost);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getProductCode() {
        return productCode == null ? "" : productCode.trim();
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return productName == null ? "" : productName.trim();
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }

    public int getPayStatus() {
        return payStatus;
    }

    public void setPayStatus(int payStatus) {
        this.payStatus = payStatus;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public List<VasOrderPassengerDto> getPassengers() {
        if (passengers == null) {
            passengers = new ArrayList<>();
        }
        return passengers;
    }

    public void setPassengers(List<VasOrderPassengerDto> passengers) {
        this.passengers = passengers;
    }

    public int getFlightOrderId() {
        return flightOrderId;
    }

    public void setFlightOrderId(int flightOrderId) {
        this.flightOrderId = flightOrderId;
    }

    public CustomerDto getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerDto customer) {
        this.customer = customer;
    }

    public int getCustomerType() {
        return customerType;
    }

    public void setCustomerType(int customerType) {
        this.customerType = customerType;
    }


    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public List<FlightBasicInfo> getFlights() {
        if (flights == null) {
            flights = new ArrayList<>();
        }
        return flights;
    }

    public void setFlights(List<FlightBasicInfo> flights) {
        this.flights = flights;
    }


    public int getProductType() {
        return productType;
    }

    public void setProductType(int productType) {
        this.productType = productType;
    }

    public String getTicketer() {
        return ticketer;
    }

    public void setTicketer(String ticketer) {
        this.ticketer = ticketer;
    }

    public int getOrderType() {
        return orderType;
    }

    public void setOrderType(int orderType) {
        this.orderType = orderType;
    }

    public String getExternalOrderNo() {
        return externalOrderNo;
    }

    public void setExternalOrderNo(String externalOrderNo) {
        this.externalOrderNo = externalOrderNo;
    }

    public String getCostCenter() {
        return costCenter;
    }

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }


    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Integer getBlanketOrderId() {
        return blanketOrderId;
    }

    public void setBlanketOrderId(Integer blanketOrderId) {
        this.blanketOrderId = blanketOrderId;
    }

    public BaseOrderDto getParentOrder() {
        return parentOrder;
    }

    public void setParentOrder(BaseOrderDto parentOrder) {
        this.parentOrder = parentOrder;
    }

    public int getInsuranceOrderType() {
        return insuranceOrderType;
    }

    public void setInsuranceOrderType(int insuranceOrderType) {
        this.insuranceOrderType = insuranceOrderType;
    }
}
